package com.CGFrame.struts.bean.myJob;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

import com.CGFrame.struts.POJOs.Category;
import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.POJOs.Task;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.project.ProjectBean;
import com.CGFrame.struts.bean.reguser.ReguserBean;
import com.CGFrame.struts.bean.task.TaskBean;
import com.CGFrame.struts.bean.unit.UnitBean;

public class myJob {
	ProjectBean projectBean;
//	Reguser reguser;
	ProjectOperate pop;
	LinkedHashSet lin_FinishedElementTasks;
	LinkedHashSet lin_FinishedShotTasks;
	public myJob(String projectId)
	{
		this.pop = ProjectOperateFactory.getprojectOperate();
		this.projectBean = new ProjectBean(projectId);
	}
	
	
	public LinkedHashSet getFinishedElementTasks()
	{
		return this.lin_FinishedElementTasks;
	}
	
	public LinkedHashSet getFinishedShotTasks()
	{
		return this.lin_FinishedShotTasks;
	}
	
	
	/**
	 * get ElementHeader of DataGrid
	 * @return
	 */
	public StringBuffer getElementHeader()
	{
		StringBuffer result= new StringBuffer("ID$TaskName$Status$StartTime$ElementName");
		List elementCategories = pop.Query("Category", "company.companyId,"+projectBean.getCompany().getCompanyId()+",categoryType,Element", "categoryId", "asc");
		int size = elementCategories.size();
		for (int i = 0;i<size;i++)
		{
			Category category = (Category)elementCategories.get(i);
			result.append("$"+category.getCategoryName()+"");
		}
		return result;
	}
	/** 
	 * get ShotHeader of DataGrid
	 * @return
	 */
	public StringBuffer getShotHeader()
	{
		StringBuffer result= new StringBuffer("ID$TaskName$Status$StartTime$ShotName");
		
		List shotCategories = pop.Query("Category", "company.companyId,"+projectBean.getCompany().getCompanyId()+",categoryType,Shot", "categoryId", "asc");
		int size = shotCategories.size();
		for (int i = 0;i<size;i++)
		{
			Category category = (Category)shotCategories.get(i);
			result.append("$"+category.getCategoryName()+"");
		}
		return result;
	}
	/**
	 * get task's Information
	 * @param task
	 * @return data
	 */
	public StringBuffer getDataByTask(Task task)
	{
		UnitBean unitBean ;
		List unitCategories = null;
		TaskBean taskBean = new TaskBean(task);
		if ( taskBean.canStart()==false ) return null;
		String yyyyMMdd = "";
		try
		{
			SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
			yyyyMMdd =  dateFm.format(taskBean.getVe());
		}
		catch (Exception e)
		{
			
		}
		StringBuffer result = new StringBuffer(""+task.getTaskId().toString()+"$"+""+task.getTaskName()+"$"+""+task.getStatus().getStatusName()+"$"+yyyyMMdd+"");
		if(task.getMake().getElement() != null) // Element task
		{
			unitBean = new UnitBean("Element",task.getMake().getElement().getElementId());
			unitCategories = pop.Query("Category", "company.companyId,"+projectBean.getCompany().getCompanyId()+",categoryType,Element", "categoryId", "asc");
			
		}
		else if(task.getMake().getShot() != null) // Shot task
		{
			unitBean = new UnitBean("Shot",task.getMake().getShot().getShotId());
			unitCategories = pop.Query("Category", "company.companyId,"+projectBean.getCompany().getCompanyId()+",categoryType,Shot", "categoryId", "asc");
		}
		else 
		{
			return null;
		}
		result.append("$"+unitBean.getUnitName()+"");
		int size = unitCategories.size();
		for (int i = 0 ; i<size ; i++)
		{
			Category category = (Category)unitCategories.get(i);
			Make make;
			if ( ( make =unitBean.getMakeByCategory(category) )==null )
			{
				result.append("$ ");
				
			}
			else
			{
				result.append("$"+make.getStatus().getStatusName()+"" );
			}
		}
		return result;
		
	}
	
	/*
	public StringBuffer addColorForData(String data)
	{
		StringBuffer result;
		//	<font color="red">data</font>  example
		if (data.equals("HasInput"))
		{
			result = new StringBuffer("<font color='pink'>"+data+"</font>" );
		}
		else if (data.equals("HasAssigned"))
		{
			result = new StringBuffer("<font color='green'>"+data+"</font>" );
		}
		else if (data.equals("IsDoing"))
		{
			result = new StringBuffer("<font color='yellow'>"+data+"</font>" );
		}
		else if (data.equals("WaitingConfirm"))
		{
			result = new StringBuffer("<font color='red'>"+data+"</font>" );
		}
		else if (data.equals("Finished"))
		{
			result = new StringBuffer("<font color='gray'>"+data+"</font>" );
		}
		else
		{
			result = new StringBuffer(data);
		}
		return result;
	}
	*/
	public LinkedHashSet getReguserElementTasks(String reguserId)
	{
		LinkedHashSet lin_tasksinformation = new LinkedHashSet() ;
		this.lin_FinishedElementTasks = new LinkedHashSet();
	//	Reguser reguser = (Reguser)pop.UniqueQuery("Reguser", "regUserId,"+reguserId);
	//	Iterator ite_tasks = reguser.getTasks().iterator();
		ReguserBean reguserBean = new ReguserBean(reguserId);
		Iterator ite_tasks = reguserBean.getTasksByProject(this.projectBean.getProjectId()).iterator();
		
		while(ite_tasks.hasNext())
		{
			Task task = (Task)ite_tasks.next();
			if (task.getMake().getElement()!= null)
			{
				if ( task.getStatus().getStatusName().equals("Finished") )
				{
					this.lin_FinishedElementTasks.add(""+task.getTaskId()+"$"+task.getTaskName()+"");
				}
				else
				{					
					StringBuffer taskinformation = this.getDataByTask(task);
					if (taskinformation!=null){
						lin_tasksinformation.add(taskinformation);
					}
				}
			}
		}
		return lin_tasksinformation;
	}
	public LinkedHashSet getReguserShotTasks(String reguserId)
	{
		LinkedHashSet lin_tasksinformation = new LinkedHashSet() ;
		this.lin_FinishedShotTasks = new LinkedHashSet();
//		Reguser reguser = (Reguser)pop.UniqueQuery("Reguser", "regUserId,"+reguserId);
//		Iterator ite_tasks = reguser.getTasks().iterator();
		ReguserBean reguserBean = new ReguserBean(reguserId);
		Iterator ite_tasks = reguserBean.getTasksByProject(this.projectBean.getProjectId()).iterator();
		while(ite_tasks.hasNext())
		{
			Task task = (Task)ite_tasks.next();
			if (task.getMake().getShot()!= null)
			{
				if (task.getStatus().getStatusName().equals("Finished"))
				{
					this.lin_FinishedShotTasks.add(""+task.getTaskId()+"$"+task.getTaskName()+"");
				}
				else
				{
					StringBuffer taskinformation = this.getDataByTask(task);
					if(taskinformation!=null){
						lin_tasksinformation.add(taskinformation);
					}
				}
			}
		}
		return lin_tasksinformation;
	}
	public  static void main(String args[])
	{
		myJob myjob = new myJob("1");
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
//		System.out.println(myjob.getShotHeader());
		Task task = (Task)pop.UniqueQuery("Task", "taskId,60");
//		System.out.println( myjob.getDataByTask(task) );
	}
}
